<?php
declare(strict_types=1);

namespace Plugin\Alarm\Http\Controller\Admin;

use Plugin\Alarm\Service\RdAlarmDangerTypeService;
use Plugin\Alarm\Http\Request\Admin\RdAlarmDangerTypeRequest;
use Plugin\Alarm\Schema\RdAlarmDangerTypeSchema;
use App\Http\Admin\Middleware\PermissionMiddleware;
use App\Http\Common\Middleware\AccessTokenMiddleware;
use App\Http\Common\Middleware\OperationMiddleware;
use App\Http\Common\Result;
use App\Http\CurrentUser;
use Hyperf\HttpServer\Request;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Swagger\Annotation\Delete;
use Hyperf\Swagger\Annotation\Get;
use Hyperf\Swagger\Annotation\HyperfServer;
use Hyperf\Swagger\Annotation\JsonContent;
use Hyperf\Swagger\Annotation\Post;
use Hyperf\Swagger\Annotation\Put;
use Hyperf\Swagger\Annotation\RequestBody;
use Mine\Access\Attribute\Permission;
use Mine\Swagger\Attributes\PageResponse;
use App\Http\Admin\Controller\AbstractController as AdminAbstractController;

/**
 * 隐患类别控制器
 * Class RdAlarmDangerTypeController
 */
#[HyperfServer(name: 'http')]
#[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
#[Middleware(middleware: PermissionMiddleware::class, priority: 99)]
#[Middleware(middleware: OperationMiddleware::class, priority: 98)]
final class RdAlarmDangerTypeController extends AdminAbstractController
{
    /**
     * 业务处理服务
     * RdAlarmDangerTypeService
     */
    public function __construct(
        protected readonly RdAlarmDangerTypeService $service,
        protected readonly CurrentUser $currentUser
    ) {}

    #[Get(
        path: '/alarm/rdAlarmDangerType/list',
        operationId: 'AlarmRdAlarmDangerTypeList',
        summary: '隐患类别列表',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['隐患类别'],
    )]
    #[Permission(code: 'alarm:rdAlarmDangerType:index')]
    #[PageResponse(instance: RdAlarmDangerTypeSchema::class)]
    public function page(Request $request): Result
    {
        return $this->success(data: $this->service->page(array_merge([

        ], $request->all()), (int) $request->query('page'), (int) $request->query('page_size')));
    }

    #[Post(
        path: '/alarm/rdAlarmDangerType',
        operationId: 'AlarmRdAlarmDangerTypeCreate',
        summary: '创建隐患类别',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['隐患类别'],
    )]
    #[RequestBody(
        content: new JsonContent(ref: RdAlarmDangerTypeRequest::class, title: '创建隐患类别')
    )]
    #[PageResponse(instance: new Result())]
    #[Permission(code: 'alarm:rdAlarmDangerType:save')]
    public function create(RdAlarmDangerTypeRequest $request): Result
    {
        $this->service->create(array_merge($request->post(), [
            'created_by' => $this->currentUser->id(),
        ]));
        return $this->success();
    }

    #[Put(
        path: '/alarm/rdAlarmDangerType/{id}',
        operationId: 'AlarmRdAlarmDangerTypeEdit',
        summary: '编辑隐患类别',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['隐患类别']
    )]
    #[RequestBody(
        content: new JsonContent(ref: RdAlarmDangerTypeRequest::class, title: '编辑隐患类别')
    )]
    #[PageResponse(instance: new Result())]
    #[Permission(code: 'alarm:rdAlarmDangerType:update')]
    public function save(int $id, RdAlarmDangerTypeRequest $request): Result
    {
        $this->service->updateById($id, array_merge($request->post(), [
            'updated_by' => $this->currentUser->id(),
        ]));
        return $this->success();
    }

    #[Delete(
        path: '/alarm/rdAlarmDangerType',
        operationId: 'AlarmRdAlarmDangerTypeDelete',
        summary: '删除隐患类别',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['隐患类别']
    )]
    #[PageResponse(instance: new Result())]
    #[Permission(code: 'alarm:rdAlarmDangerType:delete')]
    public function delete(): Result
    {
      $this->service->deleteById($this->getRequest()->all());
      return $this->success();
    }
}